﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace tsgl
{
    public partial class borrowerInfoAdd : Form
    {
        public borrowerInfoAdd()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if(this.txtBorrowerId.Text=="")
            {
                MessageBox.Show("借阅者编号不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.txtBorrowerId.Focus();
            }
            else if(this.txtBorrowerName.Text=="")
            {
               MessageBox.Show("姓名不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.txtBorrowerName.Focus();
            }
            else if(this.cboSex.Text=="")
            {
               MessageBox.Show("性别不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.cboSex.Focus();
            }
           else if(this.dtpBorthday.Text=="")
            {
               MessageBox.Show("出生日期不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
               this.dtpBorthday.Focus();
            }
           else if(this.cboBorrowerType.Text=="")
            {
               MessageBox.Show("借阅者类型不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
               this.cboBorrowerType.Focus();
            }
           else if(this.cboCardState.Text=="")
            {
               MessageBox.Show("借书证状态不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
               this.cboCardState.Focus();
            }
            else if(this.dtpCardDate.Text=="")
            {
               MessageBox.Show("办证日期不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.dtpCardDate.Focus();
            }
            else if(this.dtpLineDate.Text=="")
            {
               MessageBox.Show("有效期截止日期不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.dtpLineDate.Focus();
            }
            else if(this.txtCardId.Text=="")
            {
               MessageBox.Show("证件卡号不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.txtCardId.Focus();
            }
            else if(this.txtTel.Text=="")
            {
               MessageBox.Show("联系电话不能为空！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.txtTel.Focus();
            }
            else if (this.cboDepartment.Text == "")
            {
                MessageBox.Show("部门不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cboDepartment.Focus();
            }
            else
            {
                DataTable dt=bibliothecaClass.getBorrowerInfor(this.txtBorrowerId.Text);
                if(dt.Rows.Count==0)
                {
                Boolean addResult;
                addResult = bibliothecaClass.borrowerAdd(txtBorrowerId.Text.ToString().Trim(), txtBorrowerName.Text.ToString().Trim(), cboSex.Text.ToString().Trim(), Convert.ToDateTime(dtpBorthday.Text.ToString().Trim()), borrowerType, cboCardState.Text.ToString().Trim(), Convert.ToDateTime(dtpCardDate.Text.ToString().Trim()), Convert.ToDateTime(dtpLineDate.Text.ToString().Trim()), txtCardId.Text.ToString().Trim(), txtTel.Text.ToString().Trim(), departmentId,leftCash);
                if (addResult == true)
                {
                    MessageBox.Show("新增成功！","提示信息");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("添加失败，请重试！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtBorrowerId.Clear();
                    this.txtBorrowerId.Focus();
                    this.btnAdd.Enabled = true;
                }
                }
                else
                {
                    MessageBox.Show("该借阅者已存在，请核对该借阅者编号！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    this.txtBorrowerId.Clear();
                    this.txtBorrowerId.Focus();
                }
            }

        }

        private void txtTel_TextChanged(object sender, EventArgs e)
        {
            if(this.txtTel.Text.Trim()!="")
            {
                try
                {
                    int i = Convert.ToInt32(this.txtTel.Text.Trim());
                }
                catch (Exception)
                {
                    MessageBox.Show("电话号码只能为数字型数据！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    //this.txtTel.Text = "";
                    this.txtTel.Focus();
                }
            }
        }

        private float leftCash;
        private void txtLeftCash_TextChanged(object sender, EventArgs e)
        {
            if (this.txtLeftCash.Text.Trim() != "")
            {
                try
                {
                    leftCash = float.Parse(this.txtLeftCash.Text.Trim());
                }
                catch (Exception)
                {
                    MessageBox.Show("押金剩余只能为数字型数据！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtLeftCash.Text = "";
                    this.txtLeftCash.Focus();
                }
            }
        }

        private void borrowerInfoAdd_Load(object sender, EventArgs e)
        {
            this.dtpLineDate.Enabled = false;
            this.cboSex.Items.Add("男");
            this.cboSex.Items.Add("女");
            this.cboCardState.Items.Add("有效");
            this.cboCardState.Items.Add("挂失");
            this.cboCardState.Items.Add("停用");
            DataTable dt = bibliothecaClass.getBorrowerType();
            this.cboBorrowerType.DataSource = dt;
            this.cboBorrowerType.DisplayMember = "借阅者类型";
            this.cboBorrowerType.SelectedIndex = -1;
            DataTable dt1 = bibliothecaClass.getDepartmentName();
            this.cboDepartment.DataSource = dt1;
            this.cboDepartment.DisplayMember = "部门名称";
            this.cboDepartment.SelectedIndex = -1;
            this.btnAdd.Enabled = true;
        }

        private int borrowerType;
        private void cboBorrowerType_SelectedValueChanged(object sender, EventArgs e)
        {
            DataTable dt = bibliothecaClass.getBorrowerTypeInfo(this.cboBorrowerType.Text.ToString().Trim());
            if(dt.Rows.Count!=0)
            {
                borrowerType = int.Parse(dt.Rows[0][0].ToString());
            }
        }

        private string departmentId;
        private void cboDepartment_SelectedValueChanged(object sender, EventArgs e)
        {
            DataTable dt = bibliothecaClass.getDepartmentId(this.cboDepartment.Text.ToString().Trim());
            if (dt.Rows.Count != 0)
            {
                departmentId =dt.Rows[0][0].ToString().Trim();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}